package use_google;

import org.json.JSONObject;

import android.app.Activity;
import android.app.ProgressDialog;
import android.graphics.Bitmap;
import android.os.Bundle;

import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentActivity;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentTransaction;

import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentTransaction;
import android.support.v4.util.LruCache;
import android.widget.FrameLayout;
import android.widget.ImageView;

import com.android.volley.Request;
import com.android.volley.RequestQueue;
import com.android.volley.Response;
import com.android.volley.VolleyError;
import com.android.volley.toolbox.ImageLoader;
import com.android.volley.toolbox.ImageLoader.ImageCache;
import com.android.volley.toolbox.ImageLoader.ImageListener;
import com.android.volley.toolbox.JsonObjectRequest;
import com.android.volley.toolbox.NetworkImageView;
import com.android.volley.toolbox.Volley;
import com.example.com.skills_utf8.R;



// http://www.open-open.com/lib/view/open1380637096353.html
public class useGoogle29 extends FragmentActivity{
	
	
	NetworkImageView mNetworkImageView;
	ImageView mImageView;
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_usegoogle_29);
	
		
		
		mNetworkImageView = (NetworkImageView)findViewById(R.id.networkImageView);
		mImageView = (ImageView)findViewById(R.id.imageView);
		
		/* old
		getJSONByVolley();
		showImageByNetworkImageView();
		loadImageByVolley();
		*/
		
		//FrameLayout layout_frame = (FrameLayout)findViewById(R.id.layout_frame);
		
		
		changeTo1();
		
	}
	
	private FragmentManager mFM = null;
	Home1Fra mHome1Fra;
	
	private void changeTo1() {
		
		
		if (null == mFM)
			mFM = getSupportFragmentManager();
		FragmentTransaction ft = mFM.beginTransaction();

		
		if( mHome1Fra == null){
			mHome1Fra = new Home1Fra();    
			ft.add(R.id.layout_frame, mHome1Fra);
		}
		else{
			ft.show(mHome1Fra);
		}
		ft.commit();
		
	}
	
	
	
	
	
	  /** 
     * 利用Volley异步加载图片 
     *  
     * 注意方法参数: 
     * getImageListener(ImageView view, int defaultImageResId, int errorImageResId) 
     * 第一个参数:显示图片的ImageView 
     * 第二个参数:默认显示的图片资源 
     * 第三个参数:加载错误时显示的图片资源 
     */  
    private void loadImageByVolley(){  
    	
        String imageUrl="http://avatar.csdn.net/3/B/6/1_yangqicong11.jpg";//"http://avatar.csdn.net/6/6/D/1_lfdfhl.jpg";  
        RequestQueue requestQueue = Volley.newRequestQueue(this);  
        final LruCache<String, Bitmap> lruCache = new LruCache<String, Bitmap>(20);  
        ImageCache imageCache = new ImageCache() {  
            @Override  
            public void putBitmap(String key, Bitmap value) {  
                lruCache.put(key, value);  
            }  
  
            @Override  
            public Bitmap getBitmap(String key) {  
                return lruCache.get(key);  
            }  
        }; 
        ImageLoader imageLoader = new ImageLoader(requestQueue, imageCache);  
        ImageListener listener = ImageLoader.getImageListener(mImageView, R.drawable.ic_launcher,R.drawable.ic_launcher);  
        imageLoader.get(imageUrl, listener);  
    }  
    
	
	/** 
     * 利用NetworkImageView显示网络图片 
     */  
    private void showImageByNetworkImageView(){  
        String imageUrl="http://avatar.csdn.net/6/6/D/1_lfdfhl.jpg";  
        RequestQueue requestQueue = Volley.newRequestQueue(this);  
        final LruCache<String, Bitmap> lruCache = new LruCache<String, Bitmap>(20);  
        ImageCache imageCache = new ImageCache() {  
            @Override  
            public void putBitmap(String key, Bitmap value) {  
                lruCache.put(key, value);  
            }  
  
            @Override  
            public Bitmap getBitmap(String key) {  
                return lruCache.get(key);  
            }  
        };  
        ImageLoader imageLoader = new ImageLoader(requestQueue, imageCache);  
        mNetworkImageView.setTag("url");  
        mNetworkImageView.setImageUrl(imageUrl,imageLoader);  
    }  
    
    
    
    
    
    /** 
     * 利用Volley获取JSON数据 
     */  
    private void getJSONByVolley() {  
        RequestQueue requestQueue = Volley.newRequestQueue(this);  
        String JSONDataUrl = "http://pipes.yahooapis.com/pipes/pipe.run?_id=giWz8Vc33BG6rQEQo_NLYQ&_render=json";  
        final ProgressDialog progressDialog = ProgressDialog.show(this, "This is title", "...Loading...");  
  
        
        JsonObjectRequest jsonObjectRequest = new JsonObjectRequest(  
                Request.Method.GET,   
                JSONDataUrl,   
                null,  
                new Response.Listener<JSONObject>() {  
                    @Override  
                    public void onResponse(JSONObject response) {  
                        System.out.println("response="+response);  
                        if (progressDialog.isShowing()&&progressDialog!=null) {  
                            progressDialog.dismiss();  
                        }  
                    }  
                },   
                new Response.ErrorListener() {  
                    @Override  
                    public void onErrorResponse(VolleyError arg0) {  
                           System.out.println("sorry,Error");  
                    }  
                });  
        requestQueue.add(jsonObjectRequest);  
    }  
      
    
    
    
    
    
    
}


